package com.jyy.prefabricated.controller;


import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.entity.Student;
import com.jyy.prefabricated.entity.StudentQuestionNote;
import com.jyy.prefabricated.service.StudentQuestionNoteService;
import com.jyy.prefabricated.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 学员题目笔记
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class StudentQuestionNoteController {

    @Autowired
    private StudentQuestionNoteService studentQuestionNoteService;

    @Autowired
    private UserUtils userUtils;

    /**
     * 新增笔记
     * @param studentQuestionNote
     * @return
     */
    @PostMapping("/student-question-note")
    public AjaxResult insert(@Valid @RequestBody StudentQuestionNote studentQuestionNote){
        log.info("Rest request to insert studentQuestionNote:{}",studentQuestionNote);
        Student student = userUtils.getCurrentStudent();
        studentQuestionNote.setStudentId(student.getId());
        Integer id = studentQuestionNoteService.insert(studentQuestionNote);
        StudentQuestionNote result = studentQuestionNoteService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改笔记
     * @param studentQuestionNote
     * @return
     */
    @PutMapping("/student-question-note")
    public AjaxResult update(@Valid @RequestBody StudentQuestionNote studentQuestionNote){
        log.info("Rest request to update studentQuestionNote:{}",studentQuestionNote);
        studentQuestionNoteService.update(studentQuestionNote);
        StudentQuestionNote result = studentQuestionNoteService.selectById(studentQuestionNote.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询笔记详情
     * @param id
     * @return
     */
    @GetMapping("/student-question-note/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find studentQuestionNote by id:{}",id);
        StudentQuestionNote result = studentQuestionNoteService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除笔记
     * @param id
     * @return
     */
    @DeleteMapping("/student-question-note/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete studentQuestionNote by id:{}",id);
        int row = studentQuestionNoteService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

}
